﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace MyCef.control
{
    public partial class MyTabControl : TabControl
    {
        public MyTabControl()
        {
            InitializeComponent();
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.SizeMode = TabSizeMode.Fixed;
            this.ItemSize = new Size(100, 25);
            this.TabStop = true;
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            this.TabPages[this.SelectedIndex].Focus();
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            //计算关闭区域  
            Rectangle bounds = this.GetTabRect(this.SelectedIndex);
            bounds.Offset(bounds.Width - 20, 3);

            //如果鼠标在区域内就关闭选项卡  
            bool isClose = e.X > bounds.X && e.X < bounds.Right
             && e.Y > bounds.Y && e.Y < bounds.Bottom;
            if (isClose == true)
            {
                int index = this.SelectedIndex;
                TabPage tab = this.SelectedTab;
                this.TabPages.Remove(tab);
                tab.Dispose();
            }
        }

        private void SetStyles()
        {
            base.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            // 获取当前选项卡区域
            Rectangle bounds = GetTabRect(e.Index);
            LinearGradientBrush notSelect = new LinearGradientBrush(bounds, Color.Silver, Color.White, LinearGradientMode.Vertical);
            LinearGradientBrush select = new LinearGradientBrush(bounds, Color.SkyBlue, Color.White, LinearGradientMode.Vertical);
            Brush bt = SystemBrushes.ActiveCaptionText;
            if (e.Index == this.SelectedIndex)    //当前选中的Tab页，设置不同的样式以示选中  
            {
                e.Graphics.FillRectangle(select, bounds); //改变选中选项卡标签的背景色   
            }
            else
            {
                e.Graphics.FillRectangle(notSelect, bounds); //改变非选中的选项卡标签的背景色
                bt = SystemBrushes.GrayText;    //置灰非选中项
            }

            //绘制图标  
            if (this.ImageList != null)
            {
                int index = this.TabPages[e.Index].ImageIndex;
                string key = this.TabPages[e.Index].ImageKey;
                Image icon = new Bitmap(60, 60);
                if (index > -1)
                {
                    icon = this.ImageList.Images[index];
                }
                if (!string.IsNullOrEmpty(key))
                {
                    icon = this.ImageList.Images[key];
                }
                e.Graphics.DrawImage(icon, bounds.X + 20, bounds.Top + 2);

            }
            Font font = new Font("微软雅黑", 9f, FontStyle.Regular);
            //绘制字符
            String text = this.TabPages[e.Index].Text.Length > 5 ? this.TabPages[e.Index].Text.Substring(0, 5) + "..." : this.TabPages[e.Index].Text;
            e.Graphics.DrawString(text, font, bt, bounds.X + 5, bounds.Y + 2);

            bounds.Offset(bounds.Width - 20, 3);
            e.Graphics.DrawImage(Resource1.cross, bounds.X, bounds.Y);
        }
    }
}
